/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trab01_so;

/**
 *
 * @author Leonardo
 */
import java.util.ArrayList;
import java.util.Date;

public class MemoriaPrincipal extends ArrayList<Pagina> {

    int numQuadros;

    public boolean hasPage(String addr) {
        
        boolean a = false;
        for ( int i = 0 ; i < this.size(); i++)
        {
           if ( this.get(i).address.equalsIgnoreCase(addr))
               a = true;
        }

        return a;
    }

    public int getLRUIdx(String subst, String pid) {
        Date menorData = new Date();
        int idx = -1;
        if (subst.equalsIgnoreCase("LOCAL")) {
            for (int i = 0; i < this.size(); i++) {
                Date dtAux = this.get(i).getLastAccessDate();
                if (dtAux.before(menorData) && this.get(i).id.equalsIgnoreCase(pid)) { // leva em consideração o PID
                    menorData = dtAux;
                    idx = i;
                }
            }
        } else if (subst.equalsIgnoreCase("GLOBAL")) {
            for (int i = 0; i < this.size(); i++) {
                Date dtAux = this.get(i).getLastAccessDate();
                if (dtAux.before(menorData)) { //nao leva em consideração o PID
                    menorData = dtAux;
                    idx = i;
                }
            }
        }
        return idx;
    }
    
    public int getPageIdx(String addr){
        int idx = -1;
        for (int i = 0; i < this.size(); i++){
            if (this.get(i).address.equalsIgnoreCase(addr)){
                idx = i;
            }
        }
        return -1;
    }

    public Pagina getPage(String end) {
        Pagina p = null;
        for (int i=0;i<this.size();i++){
            if (this.get(i).getAddress().equalsIgnoreCase(end)) {
                p = this.get(i);
            }
        }
        return p;
    }
}
